package work.timejay.user.controller.files;

import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import work.timejay.common.ApiResult;
import work.timejay.utils.BigFileUploadUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @className:BigFileUploadController.java
 * @author:王文杰
 * @version:1.0.0
 * @description:大文件分片上传
 * @createTime 2022年11月17日 16:41:00
 */
@RestController
@CrossOrigin
@RequestMapping(value = "/big")
@Slf4j
@Tag(name = "大文件接口层",description = "BigFileUploadController")
public class BigFileUploadController {
    @Autowired
    private BigFileUploadUtils bigFileUploadUtils;
    /**
     * 文件上传
     */
    @PostMapping(value = "/upload")
    public void bigFileUpload(HttpServletRequest request, HttpServletResponse response) {
        log.error("进入了");
        response.setCharacterEncoding("utf-8");
        StopWatch stopWatch = new StopWatch("大文件下载");
        stopWatch.start("开始");
        try {
            bigFileUploadUtils.bigFileSliceUpload(request, response);
        } catch (IOException e) {
            e.printStackTrace();

        }
        stopWatch.stop();
        log.error("耗时：{}", stopWatch.getTotalTimeSeconds());
    }

    /**
     * 大文件存储
     */
    @PostMapping(value = "/toUpload")
    public void toBigUpload(
            String id,
            String name,
            Integer chunks,
            Integer chunk,
            MultipartFile file
    ) throws IOException, InterruptedException {
        BigFileUploadUtils.toBigUpload(name, chunks,chunk, file);
    }


}

@Data
@AllArgsConstructor
@NoArgsConstructor
class toUpload {
    String id;
    String name;
    Integer chunks;
    Integer chunk;
    MultipartFile file;
}